/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "Shared/DB/MySQL/MySQLSqlConnection.h"
#include <memory>
#include "Shared/DB/MySQL/MySQLQueryResult.h"


namespace Shared {
namespace DB {
namespace MySQL {

    MySQLSqlConnection::MySQLSqlConnection(Database& db)
        : SqlConnection(db)
        , _my(NULL)
    {

    }

    MySQLSqlConnection::~MySQLSqlConnection(void)
    {
        if (_my) {
            mysql_close(_my);
        }
    }

    

    void MySQLSqlConnection::close(void)
    {
        if (_my != NULL)
        {
            mysql_close(_my);
            _my = NULL;
        }
    }

    QueryResult *MySQLSqlConnection::query(const ::std::string &sql)
    {
        if (!mysql_query(_my, sql.c_str()))
        {
            _error = false;
            MYSQL_RES* res = mysql_store_result(_my);
            if (res != NULL)
            {
                std::auto_ptr<QueryResult> rs(new MySQLQueryResult(res, 
                    mysql_affected_rows(_my)));
                if (rs.get() && rs->nextRow()) {
                    return rs.release();
                }
            }
            return NULL;
        } 
        _error = true;
        _errCode = mysql_errno(_my);
        _errString = mysql_error(_my);
        return NULL;
    }

    bool MySQLSqlConnection::execute(const ::std::string &sql)
    {
        if (mysql_query(_my, sql.c_str())) {
            _error = true;
            _errCode = mysql_errno(_my);
            _errString = mysql_error(_my);
            return false;
        }
        _error = false;
        return true;
    }

    void MySQLSqlConnection::ping(void)
    {
        SHARED_ASSERT(_my);
        SqlConnection::Lock guard(this);
        mysql_ping(_my);
    }

    bool MySQLSqlConnection::beginTransaction(void)
    {
        return transactionCmd("START TRANSACTION");
    }

    bool MySQLSqlConnection::commitTransaction(void)
    {
        return transactionCmd("COMMIT");
    }

    bool MySQLSqlConnection::rollbackTransaction(void)
    {
        return transactionCmd("ROLLBACK");
    }

    bool MySQLSqlConnection::transactionCmd(const char *cmd)
    {
        if (mysql_query(_my, cmd))
        {
            _error = true;
            _errCode = mysql_errno(_my);
            _errString = mysql_error(_my);
            return false;
        }
        _error = false;
        return true;
    }

}}}